#include "rtc_test.h"
#include "factory_api.h"
#include "ntp.h"
#include "rtthread.h"
#include <rtdef.h>
#include <rtdevice.h>
#include <rtthread.h>
#include <drivers/rtc.h>
#include "netdev_manager.h"
#include <rtdbg.h>

#define DBG_ENABLE
#define DBG_LVL DBG_LOG
#define DBG_SECTION_NAME "rtc_test"
#include "uc_log.h"

static int factory_rtc_test_tmp(factory_test_type_e test_type, char *name)
{
    time_t first_time = 0, second_time = 0, ret = 0;
    rt_err_t getflag = 0;

    // 读取rtc时间
    getflag = rt_device_control(rt_device_find("rtc"), RT_DEVICE_CTRL_RTC_GET_TIME, &first_time);
    if (getflag == -RT_ENOSYS)
    {
        factory_test_result(test_type, name, FACTORY_TEST_FAIL, "not get rtc");
        TRACE_E("not get rtc");
        return 1;
    }
    TRACE_D("first_time is %d", first_time);

    // 延时1.5s用来判断rtc是否在走时
    rt_thread_mdelay(1500);

    // 读取rtc时间
    getflag = rt_device_control(rt_device_find("rtc"), RT_DEVICE_CTRL_RTC_GET_TIME, &second_time);
    if (getflag == -RT_ENOSYS)
    {
        factory_test_result(test_type, name, FACTORY_TEST_FAIL, "not get rtc");
        TRACE_E("not get rtc");
        return 1;
    }
    TRACE_D("second_time is %d", second_time);

    // 判断时间是否变化
    if (second_time == first_time)
    {
        factory_test_result(test_type, name, FACTORY_TEST_FAIL, "rtc not change");
        return 1;
    }
    else
    {
        factory_test_result(test_type, name, FACTORY_TEST_SUCCESS, "NULL");
        return 0;
    }
}

int factory_rtc_test(void)
{
    int ret = factory_rtc_test_tmp(RTC_TEST, factory_get_key(RTC_TEST));
    return ret;
}

MSH_CMD_EXPORT_ALIAS(factory_rtc_test, rtc_test, factory mode rtc test);